package com.chenqi.community.service.Impl;

import com.chenqi.community.dao.UserDao;
import com.chenqi.community.entity.User;
import com.chenqi.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Ardai
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class})
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<User> findAllUser() {
        return userDao.findAllUser();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public User login(String username, String password) {
        User user = userDao.findUserByUsername(username);
        if (user!=null){
            if (user.getPassword().equals(password)){
                return user;
            }
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void save(User user) {
        //默认属性设置
        user.setSign("这是一段有趣的签名~");
        //默认不是会员肯定也不是吧主了
        user.setOwner(2);
        user.setStatus(0);
        //没有上传头像使用默认头像
        if (user.getAvatar()==null || user.getAvatar().equals("")){
            user.setAvatar("/avatars/myavatar.png");
        }
        userDao.insertOneUser(user);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public User findUserInfo(Integer uid) {
        return userDao.findUserById(uid);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = {RuntimeException.class})
    public void editUserInfo(User user) {
        userDao.updateUserInfo(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void LockAndUnlock(Integer uid, Integer status) {
        userDao.UserLockAndUnlock(uid,status);
    }

    @Override
    public void editUserOwner(Integer uid, Integer owner) {
        userDao.updateUserOwner(uid,owner);
    }

    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }
}
